<template>
  <div class="h-full px-4 py-3 rounded-lg">
    <div class="flex justify-between">
      <div>
        <a-button class="ghost" @click="openModal('import')">
          导入设备台账
        </a-button>
        <!-- <a-button
          type="primary"
          ghost
          class="ml-4"
          @click="openModal('device')"
        >
          设备类型配置
        </a-button> -->
      </div>
      <div class="flex justify-end">
        <a-input
          class="_input"
          v-model:value.trim="searchForm.name"
          placeholder="请输入设备名称"
          v-filter-special-chars
          maxLength="50"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.voltageLevel"
          allow-clear
          placeholder="选择电压等级"
          :options="voltageList"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.typeName"
          allow-clear
          placeholder="选择设备类型"
          :options="deviceList"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.status"
          allow-clear
          placeholder="选择设备状态"
          :options="[
            { label: '在运', value: '在运' },
            { label: '停运', value: '停运' }
          ]"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table
        :dataSource="dataSource"
        :pagination="false"
        :columns="columns"
        :loading="loading"
      >
        <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === 'order'">
            {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <IndexModal ref="indexModalRef" @isOk="getList(true, true)" />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'

import IndexModal from './IndexModal.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { list, voltage, device } from './serve.api'

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()
const voltageList = ref<any[]>([])
const deviceList = ref<any[]>([])

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  name: '',
  voltageLevel: undefined,
  typeName: undefined,
  status: undefined
}

// 搜索参数
const searchForm = reactive({
  name: '',
  voltageLevel: undefined,
  typeName: undefined,
  status: undefined
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: '设备名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  },
  {
    title: '设备类型',
    dataIndex: 'typeName',
    key: 'typeName',
    ellipsis: true
  },
  {
    title: '电压等级',
    dataIndex: 'voltageLevel',
    key: 'voltageLevel',
    ellipsis: true
  },
  {
    title: '设备ID',
    dataIndex: 'id',
    key: 'id',
    ellipsis: true
  },
  {
    title: '设备状态',
    dataIndex: 'status',
    key: 'status',
    ellipsis: true
  },
  {
    title: '运维班组',
    dataIndex: 'operationGroup',
    key: 'operationGroup',
    ellipsis: true
  },
  {
    title: '设备主人',
    dataIndex: 'owner',
    key: 'owner',
    ellipsis: true
  }
])

// 重置
const reset = () => {
  searchForm.name = ''
  searchForm.voltageLevel = undefined
  searchForm.typeName = undefined
  searchForm.status = undefined
  getList(true, true)
}

const pagination = reactive({
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  dataSource.value = []

  const params = {
    name: searchForm.name,
    voltageLevel: searchForm.voltageLevel,
    typeName: searchForm.typeName,
    status: searchForm.status,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.name = searchFormClone.name
    params.voltageLevel = searchFormClone.voltageLevel
    params.typeName = searchFormClone.typeName
    params.status = searchFormClone.status
  }

  list(params)
    .then((res) => {
      if (res) {
        console.log(res, '设备台账管理列表')

        dataSource.value = res.list
        pagination.total = res.total

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

// 新增/编辑/删除操作
type ModalType = 'import' | 'device'
const indexModalRef = ref()

const openModal = (type: ModalType, data?: any) => {
  indexModalRef.value.showModal(type, data)
}

onMounted(() => {
  voltage().then((res) => {
    res.forEach((item: any) => {
      voltageList.value.push({
        label: item,
        value: item
      })
    })
  })

  device().then((res) => {
    res.forEach((item: any) => {
      deviceList.value.push({
        label: item,
        value: item
      })
    })
  })

  getList(true, true)
})
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;

  &:not(:first-child) {
    margin-left: 1.25rem;
  }
}
</style>
